package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.protocol.CmdEnum;
import com.motu.monstercity.protocol.Common;
import com.motu.monstercity.protocol.CsGameBuddy;
import com.motu.monstercity.protocol.CsGameChat;
import com.motu.monstercity.protocol.CsGameChat.*;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.system.SystemConstant;
import com.motu.monstercity.server.game.logic.*;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.toolset.ErrorWord;
import com.motu.vertx.module.utility.toolset.SensitivewordEngine;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.ext.auth.User;

import java.util.List;

public class ChatHandler {


    //CMD_GAME_CHAT_PUBLIC = 71351;//发送聊天
    public static Object CMD_GAME_CHAT_PUBLIC(RequestParam clientParam) throws Exception {
        CSGameChatPublicRequest request = CSGameChatPublicRequest.parseFrom(clientParam.getData());
        CSGameChatPublicResponse.Builder response = CSGameChatPublicResponse.newBuilder();
        long userId = clientParam.getUserId();
        String msg = request.getContent();
        int chatType = request.getChatType();
        int subType = request.getSubType();
        String param = request.getParam();
        UserInfo userInfo = GameUser.getUserInfo(userId);

        UserExtInfo userExtInfo = GameUser.getUserExtInfo(userId);
        if (!ChatManager.isCanChat(userInfo, userExtInfo)) {
            return ErrorWord.WRONG_STATUS;// 被禁言或是太频繁发言
        }
        // 超过最大长度自动截断
        if (msg.length() > AllParam.CHAT_CONTENT_MAX) {
            msg = msg.substring(0, AllParam.CHAT_CONTENT_MAX);
        }
        // 过滤敏感字符
        try {
            msg = SensitivewordEngine.replaceWordBySdk(msg, userExtInfo.getPlatform());
        } catch (Exception e) {
            return ErrorWord.BAD_WORD;
        }

        // 屏蔽# $符号
        msg = Tool.replaceColorChar(msg);

        int serverId = userInfo.getServerId();
        int groupId = ChatManager.getGroupId(serverId, chatType);
        Common.PBChatMsg.Builder chatMsgBuilder = Common.PBChatMsg.newBuilder();

        if (chatType == ChatManager.CHAT_TYPE_SERVER || chatType == ChatManager.CHAT_TYPE_WORLD) {
            //世界聊天
            ChatPublic cp = new ChatPublic(userInfo, groupId, serverId, msg, chatType, subType, param);
            cp.doCreate();
            chatMsgBuilder = cp.takeBuilder();
        } else if(chatType == ChatManager.CHAT_TYPE_UNION) {
            //联盟聊天
            UserUnion userUnion = UnionManager.getUserUnion(userId);
            long unionId = userUnion.getUnionId();
            if (unionId == 0) {
                return ErrorWord.INVALID_OPERATION;//没有联盟
            }
            ChatUnion chatUnion = new ChatUnion(userInfo, subType, unionId, msg, param);
            chatUnion.doCreate();
            chatMsgBuilder = chatUnion.takeBuilder();
        }

        // 广播给玩家
        BoardcastManager.publicChatMessage(userInfo, chatMsgBuilder);
        response.setChatMsg(chatMsgBuilder);
        return response;
    }

    //CMD_GAME_CHAT_PUBLIC_RECORD = 71351;//读取聊天记录
    public static Object CMD_GAME_CHAT_PUBLIC_RECORD(RequestParam clientParam) throws Exception {
        CSGameChatPublicRecordRequest request = CSGameChatPublicRecordRequest.parseFrom(clientParam.getData());
        CSGameChatPublicRecordResponse.Builder response = CSGameChatPublicRecordResponse.newBuilder();
        long userId = clientParam.getUserId();
        int chatType = request.getChatType();
        long id = request.getId();
        UserInfo userInfo = GameUser.getUserInfo(userId);
        List<ChatPublic> chatPublicList;
        int groupId = ChatManager.getGroupId(userInfo.getServerId(), chatType);
        chatPublicList = ChatManager.getChatPublicList(groupId);
        int count = 0;
        for (ChatPublic chatPublic : chatPublicList) {
            if (chatPublic.getId() < id || id == 0) {// 玩家查看聊天记录,下发略早的记录
                response.addList(chatPublic.takeBuilder());
                count++;
                if (count >= AllParam.CHAT_RECORD_LIST_LENGTH) {
                    break;
                }
            }
        }
        response.setChatType(chatType);
        return response;
    }


    //CMD_GAME_CHAT_BUDDY = 71353;//发送好友聊天
    public static Object CMD_GAME_CHAT_BUDDY(RequestParam clientParam) throws Exception {
        CSGameChatBuddyRequest request = CSGameChatBuddyRequest.parseFrom(clientParam.getData());
        CSGameChatBuddyResponse.Builder response = CSGameChatBuddyResponse.newBuilder();
        long userId = clientParam.getUserId();
        long buddyId = request.getBuddyId();//接收者ID
        String msg = request.getContent();
        int subType = request.getSubType();
        String param = request.getParam();

        UserInfo userInfo = GameUser.getUserInfo(userId);
        // 功能未开启
        if (!SystemManager.checkModuleOpen(userInfo, SystemConstant.SYSTEM_MODULE_ID_BUDDY)) {
            return ErrorWord.MODULE_LOCKED;
        }

        UserExtInfo userExtInfo = GameUser.getUserExtInfo(userId);
        if (!ChatManager.isCanChat(userInfo, userExtInfo)) {
            return ErrorWord.WRONG_STATUS;// 被禁言或是太频繁发言
        }
        // 超过最大长度自动截断
        if (msg.length() > AllParam.CHAT_CONTENT_MAX) {
            msg = msg.substring(0, AllParam.CHAT_CONTENT_MAX);
        }

        // 过滤敏感字符
        try {
            msg = SensitivewordEngine.replaceWordBySdk(msg, userExtInfo.getPlatform());
        } catch (Exception e) {
            return ErrorWord.BAD_WORD;
        }

        // 屏蔽# $符号
        msg = Tool.replaceColorChar(msg);

        //判断是否是好友
        UserBuddy userBuddy = BuddyManager.getUserBuddy(userId, buddyId);

        //TODO:需要删除最早的一条数据

        ChatBuddy chatBuddy = new ChatBuddy(userInfo, subType, buddyId, msg, param);
        chatBuddy.doCreate();


        Common.PBChatMsg.Builder chatMsgBuilder = chatBuddy.takeBuilder();
        // 推送给好友
        GameUser.pushToUser(buddyId, ChatManager.takeResponseData(chatMsgBuilder));
        response.setChatMsg(chatMsgBuilder);
        response.setBuddyId(buddyId);
        return response;
    }

    //CMD_GAME_CHAT_BUDDY_RECORD = 71354;//读取好友聊天记录
    public static Object CMD_GAME_CHAT_BUDDY_RECORD(RequestParam clientParam) throws Exception {
        CSGameChatBuddyRecordRequest request = CSGameChatBuddyRecordRequest.parseFrom(clientParam.getData());
        CSGameChatBuddyRecordResponse.Builder response = CSGameChatBuddyRecordResponse.newBuilder();
        long userId = clientParam.getUserId();
        UserInfo userInfo = GameUser.getUserInfo(userId);
        // 功能未开启
        if (!SystemManager.checkModuleOpen(userInfo, SystemConstant.SYSTEM_MODULE_ID_BUDDY)) {
            return ErrorWord.MODULE_LOCKED;
        }


        long id = request.getId();
        long buddyId = request.getBuddyId();

        List<ChatBuddy>  chatBuddyList = ChatManager.getChatBuddyList(userId,buddyId);
        int count = 0;
        for (ChatBuddy chatBuddy : chatBuddyList) {
            if (chatBuddy.getId() < id || id == 0) {// 玩家查看聊天记录,下发略早的记录
                response.addList(chatBuddy.takeBuilder());
//                count++;
//                if (count >= AllParam.CHAT_RECORD_LIST_LENGTH) {
//                    break;
//                }
            }
        }
        response.setBuddyId(buddyId);
        return response;
    }

    //CMD_GAME_CHAT_UNION_RECORD = 71355;//读取联盟聊天记录
    public static Object CMD_GAME_CHAT_UNION_RECORD(RequestParam clientParam) throws Exception {
        CSGameChatUnionRecordRequest request = CSGameChatUnionRecordRequest.parseFrom(clientParam.getData());
        CSGameChatUnionRecordResponse.Builder response = CSGameChatUnionRecordResponse.newBuilder();
        long userId = clientParam.getUserId();
        UserUnion userUnion = UnionManager.getUserUnion(userId);
        Common.PBUserData.Builder pbUserData = response.getUserBuilder();
        if (userUnion.getUnionId() != 0) {
            long unionId = userUnion.getUnionId();
            UnionInfo unionInfo = UnionManager.getUnionInfo(unionId);

            long id = request.getId();
            List<ChatUnion>  chatUnionList = ChatManager.getChatUnionList(unionId);
            int count = 0;
            for (ChatUnion chatPublic : chatUnionList) {
                if (chatPublic.getId() < id || id == 0) {// 玩家查看聊天记录,下发略早的记录
                    response.addList(chatPublic.takeBuilder());
                    count++;
                    if (count >= AllParam.CHAT_RECORD_LIST_LENGTH) {
                        break;
                    }
                }
            }
            ProtoDataUtils.updatePBUserData(pbUserData, unionInfo,true);
        }
        ProtoDataUtils.updatePBUserData(pbUserData, userUnion,true);
        return response;
    }

    //CMD_GAME_CHAT_BUDDY_READ = 71356;//设置私聊信息已读
    public static Object CMD_GAME_CHAT_BUDDY_READ(RequestParam clientParam) throws Exception {
        CsGameChat.CSGameChatBuddyReadRequest request = CsGameChat.CSGameChatBuddyReadRequest.parseFrom(clientParam.getData());
        CsGameChat.CSGameChatBuddyReadResponse.Builder response = CsGameChat.CSGameChatBuddyReadResponse.newBuilder();
        long userId = clientParam.getUserId();
        UserInfo userInfo = GameUser.getUserInfo(userId);
        // 功能未开启
        if (!SystemManager.checkModuleOpen(userInfo, SystemConstant.SYSTEM_MODULE_ID_BUDDY)) {
            return ErrorWord.MODULE_LOCKED;
        }
        long buddyId = request.getBuddyId();
        long chatId = request.getChatId();

        //判断是佛存在好友关系

        if (chatId == 0 ) {
            //一键已读
            List<ChatBuddy>  chatBuddyList = ChatManager.getChatBuddyList(userId,buddyId);
            for (ChatBuddy chatBuddy : chatBuddyList) {
                if (chatBuddy.getReceiveId() == userId && chatBuddy.getIsRead() == ChatManager.CHAT_IS_READ_NO) {
                    //接收方事自己并且消息未读 设置为已读
                    chatBuddy.putIsRead(ChatManager.CHAT_IS_READ_YES);
                    chatBuddy.update();
                }
            }
        } else {
            ChatBuddy chatBuddy = ChatManager.getChatBuddy(chatId);
            if (chatBuddy.getUserId() == buddyId &&chatBuddy.getReceiveId() == userId && chatBuddy.getIsRead() == ChatManager.CHAT_IS_READ_NO) {
                //接收方事自己并且消息未读 设置为已读
                chatBuddy.putIsRead(ChatManager.CHAT_IS_READ_YES);
                chatBuddy.update();
            }
        }
        return response;
    }
}


